<?php
namespace app\admin\controller;
use think\Db;
class Admin extends Base{
    public function adminList(){
        $list=Db::name('admin')->alias('a')->join('adminrole b', 'a.role_id = b.id','LEFT')->field('a.*,b.title')->order('id desc')->paginate(10);
        $lists=Db::name('admin')->alias('a')->join('adminrole b', 'a.role_id = b.id','LEFT')->field('a.*,b.title')->select();
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function state_off(){
    	$id=input('param.id');
        $status = Db::name('admin')->where(array('id'=>$id))->update(['status'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
    	$id=input('param.id');
        $status = Db::name('admin')->where(array('id'=>$id))->update(['status'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function admin_add(){
    	if(request()->isGet()){
            $role=Db::name('adminrole')->where('status',1)->order('id asc')->select();
            $this->assign('role',$role);
    		return $this->fetch();
    	}elseif(request()->isPost()){
			$param = input('post.');
			$data['username']=$param['username'];
			$data['password']=md5('tmd'.$param['password']);
			$data['role_id']=$param['adminRole'];
			$data['time']=time();
			if($param['status']='on'){
				$data['status']=1;
			}elseif($param['status']='off'){
				$data['status']=2;
			}
            $admin=Db::name('admin')->where('username',$param['username'])->find();
            if($admin){
                return json(['code' => 500,'msg' => '账号同名']);
            }else{
                $res=Db::name('admin')->insert($data);
                return json(['code' => 200,'msg' => '成功']);
            }
    	}
    }
    public function admin_edit(){
    	if(request()->isGet()){
			$param = input('get.');
			$user=Db::name('admin')->where('id',$param['id'])->find();
            $role=Db::name('adminrole')->where('status',1)->order('id asc')->select();
            $this->assign('role',$role);
	        $this->assign('user',$user);
    		return $this->fetch();
    	}elseif(request()->isPost()){
			$param = input('post.');
			$data['username']=$param['username'];
			$data['password']=md5('tmd'.$param['password']);
			$data['role_id']=$param['adminRole'];
			$data['time']=time();
			if($param['status']='on'){
				$data['status']=1;
			}elseif($param['status']='off'){
				$data['status']=2;
			}
            $admin=Db::name('admin')->where('username',$param['username'])->find();
            if($admin){
                if($admin['id']!=$param['id']){
                    return json(['code' => 500,'msg' => '账号同名']);
                }else{
                    $res=Db::name('admin')->where(array('id'=>$param['id']))->update($data);
                    return json(['code' => 200,'msg' => '更新成功']);
                }
            }else{
                $res=Db::name('admin')->where(array('id'=>$param['id']))->update($data);
                return json(['code' => 200,'msg' => '更新成功']);
            }
    	}
    }
    public function admin_del(){
		$param = input('post.');
        $res= Db::name('admin')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function admin_dels(){
		$param = input('post.');
        $res= Db::name('admin')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200,'msg' => '删除成功']);

    }
    //设置
    public function system_set(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('system_set')->where('id',1)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            // $data['invite_fix_scale']=$param['invite_fix_scale'];
            $data['invite_score']=$param['invite_score'];
            $data['buy_score']=$param['buy_score'];
            $data['withdraw_score']=$param['withdraw_score'];
            $data['withdraw_money_min']=$param['withdraw_money_min'];
            $data['score_rule']=$param['score_rule'];
            $data['withdraw_rule']=$param['withdraw_rule'];
            $data['introduce']=$param['introduce'];
            $data['uptime'] =time();
            $res= Db::name('system_set')->where(array('id'=>1))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
}
